package com.big.cart.controller;

import com.big.cart.service.CartService;
import com.big.common.result.Result;
import com.big.model.vo.cart.RedisCartVo;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 购物车控制器
 */
@RestController
@RequestMapping("/api/cart")
@Slf4j
public class CartController
{


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    private CartService cartService;

    @ApiOperation("redis商品添加缓存")
    @PostMapping("/AddCart")
    public Result AddCart( RedisCartVo redisCartVo){
        boolean flag= cartService.AddCart(redisCartVo);
        return Result.ok(flag);

    }
    @ApiOperation("redis商品显示")
    @GetMapping("/showCart/{uid}")
    public Result showCart( @PathVariable("uid") Long uid){
        List<RedisCartVo> map = cartService.showCart(uid);
        if(map.size() > 0){
            return  Result.ok(map);
        }
        return  Result.fail(false);
    }


    @ApiOperation("修改数量和状态")
    @PostMapping("/upCart")
    public Result upCart(RedisCartVo redisCartVo){
        boolean flag= cartService.upStatusCart(redisCartVo);
        return Result.ok(flag);
    }

    @ApiOperation("删除购物车商品")
    @PostMapping("/deCartMerchandise")
    public Result deCartMerchandise(@RequestBody  RedisCartVo redisCartVo){
        boolean flag= cartService.deCart(redisCartVo);
        return Result.ok(flag);
    }


}
